package com.example;

import android.databinding.BindingAdapter;
import android.databinding.adapters.ListenerUtil;
import android.databinding.adapters.ViewBindingAdapter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.*;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;

/**
 * Created by jc on 17-8-15.
 */

public class Adapters {

    private static final String TAG = "DataBinding";

    @BindingAdapter({"bind:imageUrl", "bind:error"})
    public static void loadImage(ImageView view, String url, Drawable error) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(R.drawable.placeholder)
                .error(error)
                .priority(Priority.HIGH);
        Glide.with(view.getContext()).asBitmap().apply(options).load(url).into(view);
    }

// 以下两个 setPaddingLeft　都可以，两个同时存在的时候，会走下面一个
    @BindingAdapter("android:paddingLeft")
    public static void setPaddingLeft(View view, int oldPadding, int newPadding) {
        Log.d(TAG, "setPaddingLeft oldPadding: " + oldPadding + ", newPadding: " + newPadding);
        if (oldPadding != newPadding) {
            view.setPadding(newPadding,
                    view.getPaddingTop(),
                    view.getPaddingRight(),
                    view.getPaddingBottom());
        }
    }

    @BindingAdapter("android:paddingLeft")
    public static void setPaddingLeft(android.view.View view, int padding) {
        Log.d(TAG, "setPaddingLeft padding: " + padding);
        view.setPadding(padding,
                view.getPaddingTop(),
                view.getPaddingRight(),
                view.getPaddingBottom());
    }

    @BindingAdapter("android:onLayoutChange")
    public static void setOnLayoutChangeListener(android.view.View view, android.view.View.OnLayoutChangeListener oldValue,
                                                 View.OnLayoutChangeListener newValue) {
        Log.d(TAG, "setOnLayoutChangeListener oldValue: " + oldValue + ", newValue: " + newValue);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (oldValue != null) {
                view.removeOnLayoutChangeListener(oldValue);
            }
            if (newValue != null) {
                view.addOnLayoutChangeListener(newValue);
            }
        }
    }

    @BindingAdapter("android:onViewAttachedToWindow")
    public static void setListener(View view, ViewBindingAdapter.OnViewAttachedToWindow attached) {
        Log.d(TAG, "set onViewAttachedToWindow");
        setListener(view, null, attached);
    }

    @BindingAdapter("android:onViewDetachedFromWindow")
    public static void setListener(View view, ViewBindingAdapter.OnViewDetachedFromWindow detached) {
        Log.d(TAG, "set onViewDetachedFromWindow");
        setListener(view, detached, null);
    }

    @BindingAdapter({"android:onViewDetachedFromWindow", "android:onViewAttachedToWindow"})
    public static void setListener(View view, final ViewBindingAdapter.OnViewDetachedFromWindow detach,
                                   final ViewBindingAdapter.OnViewAttachedToWindow attach) {
        Log.d(TAG, "set onViewDetachedFromWindow and onViewAttachedToWindow");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
            final View.OnAttachStateChangeListener newListener;
            if (detach == null && attach == null) {
                newListener = null;
            } else {
                newListener = new View.OnAttachStateChangeListener() {
                    @Override
                    public void onViewAttachedToWindow(View v) {
                        if (attach != null) {
                            attach.onViewAttachedToWindow(v);
                        }
                    }

                    @Override
                    public void onViewDetachedFromWindow(View v) {
                        if (detach != null) {
                            detach.onViewDetachedFromWindow(v);
                        }
                    }
                };
            }
            final View.OnAttachStateChangeListener oldListener = ListenerUtil.trackListener(view,
                    newListener, R.id.onAttachStateChangeListener);
            if (oldListener != null) {
                view.removeOnAttachStateChangeListener(oldListener);
            }
            if (newListener != null) {
                view.addOnAttachStateChangeListener(newListener);
            }
        }
    }
}
